#    Copyright 2011 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPIP.
#    COOPIP is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPIP is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPIP.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
IncludePath="/opt/coopip/djangoproject/include"
if(not IncludePath in sys.path):
    sys.path.append(IncludePath)


    
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime

#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopip/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject.settings
setup_environ(djangoproject.settings)
from djangoproject.models import *
from django.contrib.gis.geos import GEOSGeometry
import CIS, AMR, OMS, GIS
from django.contrib.gis.db import models
#********************Setup psycopg2 and django section********************
from django.db import transaction

import urllib
import httplib
import datetime


StartTime=datetime.datetime.now()
Alarms=NetworkDevice.objects.filter(NetworkDeviceModel__Name='DSC 1832 Alarm').filter(ActiveIndicator=True).order_by('id')
TimesTested=0
while True:
    for i in Alarms:
        try:
            i.AlarmGetStatus()
            alarmHistory=AlarmHistory.objects.filter(NetworkDevice=i).order_by('-LastUpdateDateTime')[0]
        except:
            print "Unable to Get Status for:  " + str(i.IPAddress)
    TimesTested = TimesTested + 1
    print "Times Each Alarm was queried:  " + str(TimesTested)
    print "Seconds Elasped:  " + str((datetime.datetime.now() - StartTime).seconds)

    if(datetime.datetime.now() - StartTime).seconds > 45:
        break
    while(datetime.datetime.now() - StartTime).seconds < (15 * TimesTested) - 1:
        time.sleep(1)




